package com.windward.vii.service;

import com.github.pagehelper.PageHelper;
import com.windward.vii.common.JsonResult;
import com.windward.vii.common.ResultCode;
import com.windward.vii.dao.entity.AssetUserAccount;
import com.windward.vii.dao.entity.UserAttention;
import com.windward.vii.dao.entity.UserAttentionKey;
import com.windward.vii.dao.mapper.AssetUserAccountMapper;
import com.windward.vii.dao.mapper.UserAttentionMapper;
import com.windward.vii.dao.mapper.UserMapper;
import com.windward.vii.utils.UserUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
public class UserAttentionService {

    @Resource
    UserAttentionMapper userAttentionMapper;
    @Resource
    UserMapper userMapper;
    @Resource
    AssetUserAccountMapper assetUserAccountMapper;


    public JsonResult attention(Long uid, Long attentionUid) {
        if(uid == null || uid <= 0) {
            return new JsonResult(ResultCode.PARAM_ERROR_CODE, "用户未登录");
        }
        if(attentionUid == null || attentionUid <= 0) {
            return new JsonResult(ResultCode.PARAM_ERROR_CODE, "请选择关注人");
        }

        // 查询关注关系，是否已关注
        UserAttentionKey userAttentionKey1 = new UserAttentionKey();
        userAttentionKey1.setUid(uid);
        userAttentionKey1.setAttentionUid(attentionUid);
        UserAttention dbUserAttention = userAttentionMapper.selectByPrimaryKey(userAttentionKey1);
        if(dbUserAttention != null) {
            JsonResult jsonResult = JsonResult.success("已关注");
            jsonResult.setBody(dbUserAttention.getStatus() == 2);
            return jsonResult;
        }

        // 反向关注关系
        UserAttentionKey userAttentionKey2 = new UserAttentionKey();
        userAttentionKey2.setUid(attentionUid);
        userAttentionKey2.setAttentionUid(uid);
        UserAttention ua = userAttentionMapper.selectByPrimaryKey(userAttentionKey2);

        // 存储关注关系
        UserAttention userAttention = new UserAttention();
        userAttention.setUid(uid);
        userAttention.setAttentionUid(attentionUid);
        userAttention.setCreateTime(new Date());
        if(ua != null) {
            userAttention.setStatus(2);
        } else {
            userAttention.setStatus(1);
        }
        int row1 = userAttentionMapper.insert(userAttention);


        // 更新反向关注status
        if(row1 == 1 && ua != null) {
            ua.setStatus(2);
            int row2 = userAttentionMapper.updateByPrimaryKeySelective(ua);
            if(row2 < 1) { // 此处避免使用事务，删除上面的insert
                UserAttentionKey key = new UserAttentionKey();
                key.setUid(uid);
                key.setAttentionUid(attentionUid);
                userAttentionMapper.deleteByPrimaryKey(key);
                return JsonResult.error();
            } else {
                JsonResult jsonResult = JsonResult.success("关注成功");
                jsonResult.setBody(true);
                return jsonResult;
            }
        }
        if(row1 == 1) {
            JsonResult jsonResult = JsonResult.success("关注成功");
            jsonResult.setBody(false);
            return jsonResult;
        } else {
            return JsonResult.error();
        }
    }

    public JsonResult cancelAttention(Long uid, Long attentionUid) {
        if(uid == null || uid <= 0) {
            return new JsonResult(ResultCode.PARAM_ERROR_CODE, "请重新登录");
        }
        if(attentionUid == null || attentionUid <= 0) {
            return new JsonResult(ResultCode.PARAM_ERROR_CODE, "请选择关注的人");
        }

        UserAttentionKey key = new UserAttentionKey();
        key.setUid(uid);
        key.setAttentionUid(attentionUid);
        int row = userAttentionMapper.deleteByPrimaryKey(key);

        // 更新反向关注关系
        if(row > 0) {
            UserAttentionKey userAttentionKey = new UserAttentionKey();
            userAttentionKey.setUid(attentionUid);
            userAttentionKey.setAttentionUid(uid);
            UserAttention ua = userAttentionMapper.selectByPrimaryKey(userAttentionKey);
            if(ua != null && ua.getStatus() != 1) {
                ua.setStatus(1);
                userAttentionMapper.updateByPrimaryKeySelective(ua);
            }
        }
        return row == 1 ? JsonResult.success() : JsonResult.error();
    }

    /**
     * uid是否关注了attentionUid
     * @param uid
     * @param attentionUid
     * @return
     */
    public boolean checkIsAttention(Long uid, Long attentionUid) {
        UserAttentionKey userAttentionKey = new UserAttentionKey();
        userAttentionKey.setUid(uid);
        userAttentionKey.setAttentionUid(attentionUid);
        UserAttention userAttention = userAttentionMapper.selectByPrimaryKey(userAttentionKey);
        return userAttention != null;
    }

    public JsonResult getAttentionCountByUid(Long uid) {
        if(uid == null || uid <= 0) {
            return new JsonResult(ResultCode.PARAM_ERROR_CODE, "用户ID错误");
        }
        int count = userAttentionMapper.getAttentionCountByUid(uid);
        JsonResult jsonResult = JsonResult.success();
        jsonResult.setBody(count);
        return jsonResult;
    }

    public JsonResult getFansCountByUid(Long uid) {
        if(uid == null || uid <= 0) {
            return new JsonResult(ResultCode.PARAM_ERROR_CODE, "用户ID错误");
        }
        int count = userAttentionMapper.getFansCountByUid(uid);
        JsonResult jsonResult = JsonResult.success();
        jsonResult.setBody(count);
        return jsonResult;
    }

    public JsonResult getAttentionListByUid(Long uid, Integer pageNum, Integer pageSize) {
        if(uid == null || uid <= 0) {
            return new JsonResult(ResultCode.PARAM_ERROR_CODE, "用户ID错误");
        }
        pageNum = pageNum == null ? 1 : pageNum;
        pageSize = pageSize == null ? 10 : pageSize;
        PageHelper.startPage(pageNum, pageSize);

        List<AssetUserAccount> userList = new ArrayList<>();
        List<UserAttention> attentionList = userAttentionMapper.getAttentionListByUid(uid);
        if(attentionList != null && attentionList.size() > 0) {
            List<Long> ids = new ArrayList<>(attentionList.size());
            for(UserAttention userAttention : attentionList) {
                ids.add(userAttention.getAttentionUid());
            }
            userList = assetUserAccountMapper.getListByIds(ids);
        }
        JsonResult jsonResult = JsonResult.success();
        jsonResult.setBody(UserUtils.toUserVO(userList));
        return jsonResult;
    }

    public JsonResult getFansListByUid(Long uid, Integer pageNum, Integer pageSize) {
        if(uid == null || uid <= 0) {
            return new JsonResult(ResultCode.PARAM_ERROR_CODE, "用户ID错误");
        }
        pageNum = pageNum == null ? 1 : pageNum;
        pageSize = pageSize == null ? 10 : pageSize;
        PageHelper.startPage(pageNum, pageSize);

        List<AssetUserAccount> userList = new ArrayList<>();
        List<UserAttention> attentionList = userAttentionMapper.getFansListByUid(uid);
        if(attentionList != null && attentionList.size() > 0) {
            List<Long> ids = new ArrayList<>(attentionList.size());
            for(UserAttention userAttention : attentionList) {
                ids.add(userAttention.getUid());
            }
            userList = assetUserAccountMapper.getListByIds(ids);
        }
        JsonResult jsonResult = JsonResult.success();
        jsonResult.setBody(UserUtils.toUserVO(userList));
        return jsonResult;
    }


}
